import { axios } from "../../utils/request"
Page({
  data: {
    active:'0',
    initData:[],
    keyword:"",
  },
  async init(){
    const {data} = await axios("/raiders","GET") as {data:any} 
    const reultList =  data.data.map((item:any)=>{
      item.createTime = item.createTime.slice(0,10)
      item.cover = item.pics[0].url
      return item
    })
    this.setData({
      initData:reultList
    })
  },
  async changeCategory(e:any){
    let index = e.currentTarget.dataset.item
    this.setData({
      active:index
    })
    if(index=='0'){
      this.init()
    }else if(index=='1'){
      const { data } =  await axios("/raiders/hot") as any
      const reultList =  data.data.map((item:any)=>{
        item.createTime = item.createTime.slice(0,10)
        item.cover = item.pics[0].url
        return item
      })
      this.setData({
        initData:reultList
      })
    }else{
      const { data } =  await axios("/raiders/time") as any
      const reultList =  data.data.map((item:any)=>{
        item.createTime = item.createTime.slice(0,10)
        item.cover = item.pics[0].url
        return item
      })
      this.setData({
        initData:reultList
      })
    }
  },
  getContext(e:any){
    let item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '/pages/context/context',
      success:(res)=> {
        // 通过 eventChannel 向被打开页面传送数据
        res.eventChannel.emit('contextData', { data: this.data.initData[item],isStart:false})
      }
    })
  },
  onChange(e:any){
    if(e.detail==""){
      this.init()
    }
    this.setData({
        keyword:e.detail
    }) 
  },
  async onSearch(){
    const {data} =  await axios("/raiders/search","GET",{keyword:this.data.keyword}) as {data:any} 
    const reultList =  data.data.map((item:any)=>{
      item.createTime = item.createTime.slice(0,10)
      item.cover = item.pics[0].url
      return item
    })
    this.setData({
      initData:reultList
    })
  },
  async onShow() {
    this.getTabBar().setData({
      active:3
    })
    this.init()
  },
})